package com.lotusfall.mysport;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lotusfall.mysport.controller.TrackController;
import com.lotusfall.mysport.entity.Track;

import org.json.JSONArray;
import org.json.JSONException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TrackList extends AppCompatActivity {
    TrackController trackController = new TrackController();
    List<Track> tracks = new ArrayList<>();
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    Button btn_replayTrack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_track_list);
        mRecyclerView = findViewById(R.id.tracklist);
        JSONArray jsonArray = trackController.getTrackList();

        for (int i = 0; i < jsonArray.length(); i++) {
            Track track = new Track();
            try {
                JSONArray temp = (JSONArray) jsonArray.get(i);
                track.track_id = Long.parseLong(temp.get(0).toString());

                Long date = Long.parseLong(temp.get(1).toString());
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String sd = sdf.format(new Date(Long.parseLong(String.valueOf(date))));      // 时间戳转换成时间
                track.startDate = sd;
                track.dist_all = temp.get(2).toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            tracks.add(track);
        }
        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(TrackList.this);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(TrackList.this, R.layout.item_layout, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            btn_replayTrack = (Button) view.findViewById(R.id.btn_replayTrack);
            btn_replayTrack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = myViewHoder.getAdapterPosition();
                    Bundle bundle = new Bundle();
                    bundle.putLong("trackId",tracks.get(position).track_id);
                    Intent intent = new Intent(TrackList.this,replayTrack.class);
                    intent.putExtras(bundle);
                    startActivity(intent);

                }
            });
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            Track track = tracks.get(position);
            holder.mTitleTv.setText("开始时间：" + track.startDate);
            holder.mTitleContent.setText("跑步距离：" + String.format("%.2f", Double.parseDouble(track.dist_all)) + "m");
        }

        @Override
        public int getItemCount() {
            return tracks.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleTv;
        TextView mTitleContent;
        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            mTitleTv = itemView.findViewById(R.id.textView);
            mTitleContent = itemView.findViewById(R.id.textView2);
        }
    }
}